const app = getApp()
import http from "../../utils/request.js";
const publicFun = require('../../utils/public');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        x: 330,
        y: 250,
        height: 0, //屏幕高度
        TabCur: 0,
        currPage: 1,
        tabNav: ['公开梦境', '私密梦境'],
        dreamList: [], //梦境列表
        loading: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            height: wx.getSystemInfoSync().windowHeight,
        })
        // this.getDream()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 2
            })
        }
        this.setData({
            dreamList: []
        })
        this.getDream(false)
    },

    tabSelect(e) {
        this.setData({
            TabCur: e.detail.index,
        });
        this.getDream(false)
    },

    // 去记梦
    toWrite() {
        wx.navigateTo({
            url: '/page/writeDream/writeDream',
        })
    },

    //跳转到梦境详情
    toDetail(e) {
        wx.navigateTo({
            url: '/page/dreamDetail/dreamDetail?id=' + e.currentTarget.dataset.id,
        })
    },

    //查询梦境
    getDream(isBottom) {
        if (!isBottom) {
            this.setData({
                dreamList: [],
                currPage: 1,
            })
        } else {
            this.setData({
                currPage: this.data.currPage + 1,
            })
        }
        this.setData({
            loading: true,
        })
        let data = {
            currUserId: app.globalData.userId,
            userId: app.globalData.userId,
            pageSize: 10,
            currPage: this.data.currPage,
            dreamAuth: this.data.TabCur === 0 ? 2 : 1
        }
        http.Request({
            url: 'getDream',
            method: 'GET',
            data: data
        }).then(res => {
            if (res.code == 200) {
                if (isBottom) {
                    if (res.data.length != 0) {
                        this.setData({
                            dreamList: this.data.dreamList.concat(res.data),
                            loading: false,
                        });
                    } else {
                        this.setData({
                            loading: false,
                        });
                    }
                } else {
                    this.setData({
                        dreamList: res.data,
                        loading: false,
                    });
                }
            }
        });
    },

    // 编辑梦境
    toEdit(e) {
        wx.navigateTo({
            url: '/page/writeDream/writeDream?dreamId=' + e.currentTarget.dataset.id,
        })
    },

    // 删除梦境
    toDelete(e) {
        let that = this
        wx.showModal({
            title: '提示',
            content: '确认要删除该梦境?',
            success: function (res) {
                if (res.confirm) {
                    console.log(e.currentTarget.dataset.id);
                    let data = {
                        id: e.currentTarget.dataset.id,
                        userId: app.globalData.userId
                    }
                    http.Request({
                        url: 'delDream',
                        method: 'POST',
                        data: data
                    }).then(res => {
                        if (res.code == 200) {
                            that.getDream(false)
                            publicFun.Toast(res.msg)
                        } else {
                            publicFun.Toast('服务器错误')
                        }
                    });
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.getDream(false)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getDream(true)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})